package com.wzq.controller.pc;


import com.wzq.dao.dto.NowDiscussDto;
import com.wzq.dao.pojo.Code;
import com.wzq.dao.pojo.Discuss;
import com.wzq.dao.pojo.R;
import com.wzq.dao.pojo.Result;
import com.wzq.service.IDiscussService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/PC/discuss")
public class PCDiscussController {
    @Autowired
    private IDiscussService IDiscussService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Discuss discuss) {
        boolean flag = IDiscussService.save(discuss);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    //   修改
    @PutMapping
    public Result update(@RequestBody Discuss discuss) {
        boolean flag = IDiscussService.updateById(discuss);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }
    //    删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id ) {
        boolean flag = IDiscussService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    //    删除多个id评论
    @DeleteMapping
    public Result deleteById(@RequestBody List<Integer> ids) {
        boolean flag = IDiscussService.removeByIds(ids);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    //     获得所有评论
    @GetMapping
    public R getAll() {
        List<Discuss> list = IDiscussService.list();
        List<Discuss> list1 = new ArrayList<>();
        for (Discuss n:list ) {
            n.setPuttime(changetime(n.getPuttime()));
            list1.add(n);
        }
//        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
//        String msg = list != null ? "" : "数据查询失败，请重试！";
//        return new Result(code,list,msg);
        Map<String,Object> map=new HashMap<>();
        map.put("message",list1);
        return R.ok(map);

    }
    //  活动actid对应活动评论
    @GetMapping("/nowDiscuss/{actid}")
    public Result getByActId(@PathVariable Integer actid) {
        List<NowDiscussDto> list = IDiscussService.getByActId(actid);
        List<NowDiscussDto> list1 = new ArrayList<>();
        for (NowDiscussDto n:list ) {
            n.setPuttime(changetime(n.getPuttime()));
            list1.add(n);
        }
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list1,msg);
    }
//
    private static String changetime(String time){
        String[] split = time.substring(0, 10).split("-");
        String s =split[0] + "-"+ split[1] + "-" + split[2] ;
        return s;
    }
}

